package quickstart.perspective

import groovy.transform.CompileStatic
import javafx.event.Event
import javafx.geometry.Orientation
import javafx.scene.control.Label
import javafx.scene.control.SplitPane
import javafx.scene.layout.GridPane
import javafx.scene.layout.Priority
import org.jacpfx.api.annotations.Resource
import org.jacpfx.api.annotations.lifecycle.OnHide
import org.jacpfx.api.annotations.lifecycle.OnShow
import org.jacpfx.api.annotations.lifecycle.PostConstruct
import org.jacpfx.api.annotations.lifecycle.PreDestroy
import org.jacpfx.api.annotations.perspective.Perspective
import org.jacpfx.api.message.Message
import org.jacpfx.api.util.ToolbarPosition
import org.jacpfx.rcp.componentLayout.FXComponentLayout
import org.jacpfx.rcp.componentLayout.PerspectiveLayout
import org.jacpfx.rcp.context.Context
import org.jacpfx.rcp.perspective.FXPerspective

import java.util.logging.Logger

import static quickstart.config.BasicConfig.*

/**
 * Created by TK-chenbin on 2015/5/20.
 *
 * @author Misty
 */
@CompileStatic
@Perspective(id = PERSPECTIVE_ONE, name = "contactPerspective",
        components = [COMPONENT_LEFT, COMPONENT_RIGHT],
        resourceBundleLocation = "bundles.languageBundle")
class PerspectiveOne implements FXPerspective {
    private Logger log = Logger.getLogger(PerspectiveOne.class.name)

    @Resource
    private Context context

    private SplitPane mainLayout

    @Override
    void handlePerspective(final Message<Event, Object> message,
                           final PerspectiveLayout perspectiveLayout) {
    }

    @OnShow
    void onShow(final FXComponentLayout layout) {
        log.info("on show of PerspectiveOne")
    }

    @OnHide
    void onHide(final FXComponentLayout layout) {
        log.info("on hide of PerspectiveOne")
    }

    @PostConstruct
    void onStartPerspective(final PerspectiveLayout perspectiveLayout,
                            final FXComponentLayout layout,
                            final ResourceBundle resourceBundle) {
        def toolbar = layout.getRegisteredToolBar(ToolbarPosition.NORTH)
        toolbar.add(new Label(resourceBundle.getString("p1.button")))

        def leftMenu = new GridPane()
        def mainContent = new GridPane()

        mainLayout = new SplitPane()
        mainLayout.orientation = Orientation.HORIZONTAL
        mainLayout.items.addAll(leftMenu, mainContent)
        mainLayout.setDividerPosition(0, 0.3)
        GridPane.setHgrow(mainLayout, Priority.ALWAYS)
        GridPane.setVgrow(mainLayout, Priority.ALWAYS)

        perspectiveLayout.registerRootComponent(mainLayout)
        perspectiveLayout.registerTargetLayoutComponent(TARGET_CONTAINER_LEFT, leftMenu)
        perspectiveLayout.registerTargetLayoutComponent(TARGET_CONTAINER_MAIN, mainContent)

        log.info("on PostConstruct of PerspectiveOne")
    }

    @PreDestroy
    void onTearDownPerspective(final FXComponentLayout layout) {
        log.info("on PreDestroy of PerspectiveOne")
    }
}
